<?php
namespace frontend\models;

use common\helpers\TStringHelper;
use yii\base\Model;
use yii\base\InvalidParamException;
use common\models\User;
use Yii;

/**
 * Password reset form
 */
class ChangePasswordForm extends Model
{
    public $password_origin;
    public $password_new;
    public $password_repeat;

    public $error_message;

    /**
     * @var \common\models\User
     */
    private $_user;


    /**
     * Creates a form model given a token.
     *
     * @param string $token
     * @param array $config name-value pairs that will be used to initialize the object properties
     * @throws \yii\base\InvalidParamException if token is empty or not valid
     */
    public function __construct($config = [])
    {
        $userId = Yii::$app->user->getId();
        $this->_user = User::findOne($userId);
        if (!$this->_user) {
            throw new InvalidParamException('用户不存在');
        }
        parent::__construct($config);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['password_origin', 'password_new', 'password_repeat'], 'required'],
            [['password_origin', 'password_new', 'password_repeat'], 'string', 'min' => 6, 'max' => 30],
            ['password_repeat', 'compare', 'compareAttribute' => 'password_new', 'message' => Yii::t('frontend', 'password_repeat_error')],
        ];
    }

    public function attributeLabels()
    {
        return [
            'password_origin' => Yii::t('frontend', 'password_origin'),
            'password_new' => Yii::t('frontend', 'password_new'),
            'password_repeat' => Yii::t('frontend', 'password_repeat'),
        ];
    }

    /**
     * Resets password.
     *
     * @return boolean if password was reset.
     */
    public function resetPassword()
    {
        $user = $this->_user;
        $oldPasswordHash = $user->password_hash;
        $safety_score = TStringHelper::CheckPasswordStrength($this->password_new);
        if ($safety_score < 2) {
            return ['result' => 'other', 'message' => Yii::t('frontend', 'unsafe_passwords')];
        }

        if (Yii::$app->security->validatePassword($this->password_origin, $oldPasswordHash) === false) {
            return ['result' => 'other', 'message' => Yii::t('frontend', 'password_origin_error')];
        }


        if ($this->password_new != $this->password_repeat) {
            return ['result' => 'other', 'message' => Yii::t('frontend', 'password_repeat_error')];
        }
        if ($this->password_new == $this->password_origin) {
            return ['result' => 'other', 'message' => Yii::t('frontend', 'password_eq_error')];
        }

        $user->setPassword($this->password_new);
        $user->safety_score = $safety_score;
        //$user->removePasswordResetToken();
        if ($user->save()) {
            //Yii::$app->getSession()->setFlash('success', 'New password was saved.');
            return ['result' => 'success'];
        } else {
            return ['result' => 'failure'];
        }
    }
}
